package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.MyOrderPagerAdapter;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.ForbidScrollViewpager;

import butterknife.BindArray;
import butterknife.BindView;

public class MyOrderActivity extends BasePersonActivity {

    @BindArray(R.array.MyOrderTitle)
    String[] myOrderTitles;
    @BindView(R.id.tablayout_activity_my_order)
    TabLayout mTablayoutActivityMyOrder;
    @BindView(R.id.forbidScroll_viewpager_activity_my_order)
    ForbidScrollViewpager mForbidScrollViewpagerActivityMyOrder;
    private String mUserId;
    private int mPositon;

    @Override
    protected View getContentView(Context context) {
        //   setContentView(R.layout.activity_my_order);
        return UIUtils.inflate(this, R.layout.activity_my_order);
    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("我的订单");
        Intent intent = getIntent();
        mUserId = intent.getStringExtra(Contans.INTENT_DATA);
        mPositon = intent.getIntExtra(Contans.INTENT_EXTRA,0);
        mForbidScrollViewpagerActivityMyOrder.setOffscreenPageLimit(myOrderTitles.length);
        MyOrderPagerAdapter myOrderPagerAdapter=new MyOrderPagerAdapter(getSupportFragmentManager(),myOrderTitles,mUserId);
        mForbidScrollViewpagerActivityMyOrder.setAdapter(myOrderPagerAdapter);

        //将ViewPager关联到TabLayout上
        //   mForbidscrollViewpagerClassifyFragment.setOffscreenPageLimit(data.size());

        mTablayoutActivityMyOrder.setupWithViewPager(mForbidScrollViewpagerActivityMyOrder);

        mTablayoutActivityMyOrder.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //切换ViewPager
                mForbidScrollViewpagerActivityMyOrder.setCurrentItem(tab.getPosition(),false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        mTablayoutActivityMyOrder.getTabAt(mPositon).select();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mUserId = intent.getStringExtra(Contans.INTENT_DATA);
        mPositon = intent.getIntExtra(Contans.INTENT_EXTRA,0);
        mTablayoutActivityMyOrder.getTabAt(mPositon).select();
    }

    /**
     *
     * @param context
     * @param userid
     * @param postion 0 全部 1 待付款 2 待收货 3 已完成 4 已取消
     */

    public static void startActivity(Context context,String userid,int postion ){
        Intent intent=new Intent(context,MyOrderActivity.class);
        intent.putExtra(Contans.INTENT_DATA,userid);
        intent.putExtra(Contans.INTENT_EXTRA,postion);
        context.startActivity(intent);
    }
}
